#ifndef _DICTIONARY_H_
#define _DICTIONARY_H_

#include <map>
#include <iostream>
#include <string>

// TODO: Add a stoplist to the Dictionary (if it is really needed)
// TODO: Dictionary should save "stemmed" words

class Dictionary
{
	
private:
	
	std::map<std::string,int> words;
	
public:

	int add_word(std::string word)
	{
		if (words[word] == 0)
			words[word] = words.size();
			
		return words[word];
	}
	
	int size() const
	{
		return words.size();
	}
	
	int operator[](std::string word)
	{
		if(words.find(word) == words.end())
			return 0;
		else
			return words[word];
	}
	
	std::string operator[](int index)
	{	
		bool found = false;
		
		std::map<std::string, int>::const_iterator i = words.begin();
		while(i != words.end() && !found) 
		{
			if(i->second == index)
				found = true;
			else
				i++;
		}

		if(found)
			return i->first;
		else
			return "";
	}
	
	friend std::ostream& operator << (std::ostream &os, const Dictionary& d)
	{
		os << d.words.size();
		os << ' ';
		std::map<std::string, int>::const_iterator i; 
		for(i = d.words.begin(); i != d.words.end(); ++i)
		{
			os << i->first << ' ' << i->second << ' ';
		}
		
		return os;			
	}

	friend std::istream& operator >> (std::istream &is, Dictionary& d) 
	{
		int size = 0;
		std::string key; // a word
		int value;		 // and the correspondent index
		d.words.clear();
				
		is >> size;
		
		for(int i = 0; i < size; i++) 
		{
			is >> key;
			is >> value;
			
			d.words[key] = value;
		}

		return is;
	}
};
#endif /* _DICTIONARY_H_ */
